#ifndef EventDispatcher_h__
#define EventDispatcher_h__
#ifdef WIN32
#pragma once
#endif

#include "System/RefObj.h"
#include "Event/Event.h"
#include <map>
#include <list>
#include <boost/function.hpp>
#include "Thread/SectionLocker.h"


namespace gee
{
	class EventDispatcher ;

	typedef boost::function<void (EventDispatcher*,Event*)> EventFunc;

	class EventListener
	{
	private:
		EventFunc m_func;
	public:
		EventListener(EventFunc f)
		{
			m_func = f;
		}

		void CallEvent(EventDispatcher* sender,Event* e)
		{
			if(m_func)
			{
				m_func(sender,e);
			}
		}

		bool operator==(EventFunc r)
		{
			return &m_func == &r;
		}

	};

	class EventDispatcher :
		public RefObj
	{
		SectionLocker handlersLocker;

		typedef std::map<std::string,std::list<EventListener> > TEventListenMap;
		TEventListenMap	event_map;
	public:
		EventDispatcher(void);
		virtual ~EventDispatcher(void);


	public:
		void handleEvent(Event* e);
		bool hasEventListener(const std::string& eventType);
		void addEventListener(const char* eventType,EventFunc f);
		void addEventListener(std::string& eventType,EventFunc f);
		void removeEventListener(std::string& eventType,EventFunc f);
		void dispatchEvent(Event* e);
	};


}
#endif // EventDispatcher_h__
